using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using HandyControl.Data;

namespace Sensor.UI.Converters;

/// <summary>
/// 布尔值到 HandyControl Badge 状态的转换器
/// </summary>
/// <remarks>
/// 将布尔值转换为对应的 Badge 状态显示
/// true: BadgeStatus.Processing (处理中状态)
/// false: BadgeStatus.Dot (点状状态)
/// </remarks>
public class BoolToHCBadgeStatus : IValueConverter
{
    #region IValueConverter 实现

    /// <summary>
    /// 将布尔值转换为 Badge 状态
    /// </summary>
    /// <param name="value">要转换的布尔值</param>
    /// <param name="targetType">目标类型</param>
    /// <param name="parameter">转换参数</param>
    /// <param name="culture">区域信息</param>
    /// <returns>转换后的 Badge 状态</returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ToValue(value);
    }

    /// <summary>
    /// 将 Badge 状态转换回布尔值
    /// </summary>
    /// <param name="value">要转换的 Badge 状态</param>
    /// <param name="targetType">目标类型</param>
    /// <param name="parameter">转换参数</param>
    /// <param name="culture">区域信息</param>
    /// <returns>转换后的布尔值</returns>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return ToValue(value);
    }

    #endregion

    #region 私有方法

    /// <summary>
    /// 将值转换为对应的 Badge 状态
    /// </summary>
    /// <param name="value">要转换的值</param>
    /// <returns>转换后的 Badge 状态</returns>
    /// <remarks>
    /// 如果值为 true，返回 BadgeStatus.Processing (处理中状态)
    /// 如果值为 false，返回 BadgeStatus.Dot (点状状态)
    /// 如果值无法解析，返回 BadgeStatus.Dot (默认状态)
    /// </remarks>
    private static BadgeStatus ToValue(object value)
    {
        if (value != null && bool.TryParse(value.ToString(), out bool result))
        {
            return result ? BadgeStatus.Processing : BadgeStatus.Dot;
        }
        return BadgeStatus.Dot;
    }

    #endregion
}
